package com.xiguthings.waka.service.growth.impl;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.xiguthings.waka.dao.DataBaseStatus;
import com.xiguthings.waka.dao.extmapper.GrowthExmpleExt;
import com.xiguthings.waka.dao.extmapper.GrowthExtMapper;
import com.xiguthings.waka.exception.BussinessException;
import com.xiguthings.waka.po.Growth;
import com.xiguthings.waka.po.GrowthExample;
import com.xiguthings.waka.po.extpo.GrowthExt;
import com.xiguthings.waka.po.extpo.Page;
import com.xiguthings.waka.publicUtils.ClassUtils;
import com.xiguthings.waka.publicUtils.StringUtils;
import com.xiguthings.waka.service.growth.IGrowthService;
import com.xiguthings.waka.web.controller.base.AbstractController;

//@Transactional(readOnly = false, isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED)
@Service
@Transactional
public class GrowthServiceImpl implements IGrowthService {

    // @Resource
    // private GrowthMapper growthMapper;
    @Resource
    private GrowthExtMapper growthExtMapper;

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public int countByExample(GrowthExample example) throws Exception {
        int count = growthExtMapper.countByExample(example);
        return count;
    }

    @Override
    @Transactional(readOnly = false, isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED)
    public int insert(Growth growth) throws Exception {
        String id = StringUtils.getRandomUUID();
        growth.setGrowthId(id);
//		growth.setStatus(DataBaseStatus.NORMAL);
        int insert = growthExtMapper.insert(growth);
        return insert;
    }

    @Override
    @Transactional(readOnly = false, isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED)
    public int updateByPrimaryKeySelective(Growth record) throws Exception {
        // 判断是否所有的参数都为空
        Set<String> outFields = new HashSet<>();
        outFields.add("growthId");
        boolean flag = ClassUtils.fieldsHasValues(record, outFields);
        if (flag == false) {
            BussinessException bet = new BussinessException("修改失败，参数为空");
            bet.setCode(AbstractController.CODE_EMPTY_REQUIRED);
            throw bet;
        }

        int row = growthExtMapper.updateByPrimaryKeySelective(record);
        if (row == 0) {
            BussinessException bet = new BussinessException("修改失败，数据库中不存在这条数据！");
            bet.setCode(-1);
            throw bet;
        }
        return row;
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public Growth selectByPrimaryKey(String growthId) throws Exception {
        return growthExtMapper.selectByPrimaryKey(growthId);
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public GrowthExt selectByPrimaryKeyWithAssociated(String growthId) throws Exception {
        return growthExtMapper.selectByPrimaryKeyWithAssociated(growthId);
    }


//	@Override
//	public Page<Growth> selectByPage(Page<Growth> pageEntity, GrowthExample example) throws Exception {
//		// TODO Auto-generated method stub
//		return null;
//	}

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public Page selectByPage(Page pageEntity, GrowthExample example) throws Exception {
        if (pageEntity == null) {
            pageEntity = new Page();
        }
        int page = pageEntity.getPage();

        int rows = pageEntity.getRows();


        GrowthExmpleExt examapleExt = new GrowthExmpleExt(example);
        examapleExt.setStartRow((page - 1) * rows);
        examapleExt.setRows(rows);
        List<?> list = growthExtMapper.selectByPage(examapleExt);
        pageEntity.setList(list);

        int count = growthExtMapper.countByExample(example);
        pageEntity.setTotalCount(count);

        return pageEntity;
    }

    @Override
    public int deleteByExample(GrowthExample example) throws Exception {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public int deleteByPrimaryKey(String id) throws Exception {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public int insertSelective(Growth t) throws Exception {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public List<Growth> selectByExample(GrowthExample example) throws Exception {

        return growthExtMapper.selectByExample(example);
    }

    @Override
    public int updateByExampleSelective(Growth t, GrowthExample example) throws Exception {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public int updateByExample(Growth t, GrowthExample example) throws Exception {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public int updateByPrimaryKey(Growth t) throws Exception {
        // TODO Auto-generated method stub
        return 0;
    }


}
